package org.apache.struts.ehr.medical_records.action;

import java.util.Map;

import com.earogya.Constants;
import com.earogya.communicate.Communicator;
import com.earogya.logic.medical_records.CreateADTA02;
import com.earogya.logic.medical_records.CreateQRYA19;
import com.earogya.logic.medical_records.ParseReply;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.axis2.AxisFault;
import org.apache.struts.ehr.medical_records.model.HL7Message;
import org.apache.struts.ehr.medical_records.model.Query;
import org.apache.struts.ehr.medical_records.model.ReplyMessage;
import org.apache.struts.ehr.medical_records.model.TransferToAnotherWard;
import org.apache.struts.ehr.medical_records.model.Ward;

/***
 * This class is the struts action to transfer a patient to
 * another ward or another bed in the same ward.
 * @author Chamendri
 *
 */
public class TransferPatientAction extends ActionSupport{

	private TransferToAnotherWard transferToAnotherWard;
	private HL7Message message;
	private ReplyMessage replyMessage;
	private Ward ward;
	private String occupiedBeds;
	private Boolean isReply=false;
	private Boolean isWardEmpty=false;
	
	public Boolean getIsWardEmpty() {
		return isWardEmpty;
	}

	public Boolean getIsReply() {
		return isReply;
	}


	public String getOccupiedBeds() {
		return occupiedBeds;
	}

	public Ward getWard() {
		return ward;
	}

	public void setWard(Ward ward) {
		this.ward = ward;
	}

	public HL7Message getMessage() {
		return message;
	}
	
	public ReplyMessage getReplyMessage() {
		return replyMessage;
	}

	public TransferToAnotherWard getTransferToAnotherWard() {
		return transferToAnotherWard;
	}

	public void setTransferToAnotherWard(TransferToAnotherWard transferToAnotherWard) {
		this.transferToAnotherWard = transferToAnotherWard;
	}

	/***
	 * overrides the execute method in ActionSupport interface.
	 * Creates a ADT A02 message with the parameter "ward".
	 * returns SUCCESS or ERROR.
	 */
	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		replyMessage=new ReplyMessage();
		replyMessage.setMessage(Constants.EMPTY);
		CreateADTA02 createMessage=new CreateADTA02(transferToAnotherWard);
		String msg=createMessage.createMessage(Constants.SENDING_FACILITY, Constants.TRANSFER_WARD);
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			if(!reply.equals(Constants.NO_REPLY)){
				ParseReply parse=new ParseReply();
				replyMessage=(ReplyMessage)parse.parseMessage(reply);
				if(replyMessage.getMessage().equals(Constants.SUCCESS)){
					Query query1=new Query();
					query1.setPatientId(transferToAnotherWard.getToWardNo());
					query1.setSearch_type(Constants.OCCUPIED_BEDS);
					CreateQRYA19 qry=new CreateQRYA19(query1);
					String msg1=qry.createMessage(Constants.SENDING_FACILITY);
					if(!msg1.equals(null)){
						Communicator c1=new Communicator();
						try {
							String reply1=c1.sendMessage(msg1);
							if(!reply.equals(Constants.NO_REPLY)){
								ParseReply parse1=new ParseReply();
								occupiedBeds=(String)parse1.parseMessage(reply1);
								if(occupiedBeds!=null){
									isReply=true;
								}else{
									isWardEmpty=true;
									isReply=true;
								}
								ward=new Ward();
								ward.setWardNo(Integer.parseInt(transferToAnotherWard.getToWardNo()));
							}else{
								replyMessage.setMessage(reply1);
							}
						} catch (AxisFault e) {
							replyMessage.setMessage(e.getReason());
							return ERROR;
						} catch (Exception e) {
							replyMessage.setMessage(e.getMessage());
							return ERROR;
						}
					}else{
						replyMessage.setMessage("Failed to create request message");
					}
				}else{
					return ERROR;
				}
			}
			else{
				replyMessage.setMessage(reply);
				return ERROR;
			}
		}
		else{
			replyMessage.setMessage("Failed to create request message");
		}
		replyMessage.setMessage("Patient id "+transferToAnotherWard.getPatientId()+" is successfully transfered from ward :"+transferToAnotherWard.getFromWardNo()+" bed :"+transferToAnotherWard.getFromBedNo()+" to ward :"+transferToAnotherWard.getToWardNo()+" bed :"+transferToAnotherWard.getToBedNo());
		return SUCCESS;
	}
	
	/**
	 * Overrides validate method in ActionSupport interface
	 * validates input fields in transfer to another ward form.
	 */
	public void validate(){
		if(transferToAnotherWard.getPatientId().length()==0){
			addFieldError("transferToAnotherWard.patientId", "This field is required");
		}
		if(transferToAnotherWard.getPatientId().length()!=0 && !this.isInteger(transferToAnotherWard.getPatientId())){
			addFieldError("transferToAnotherWard.patientId", "Integer required");
		}
		if(transferToAnotherWard.getHospital().length()==0){
			addFieldError("transferToAnotherWard.hospital", "This field is required");
		}
		if(transferToAnotherWard.getTransferTime()==null){
			addFieldError("transferToAnotherWard.transferTime", "This field is required");
		}
		if(transferToAnotherWard.getFromBedNo().length()==0){
			addFieldError("transferToAnotherWard.fromBedNo", "This field is required");
		}
		if(transferToAnotherWard.getFromBedNo().length()!=0 && !this.isInteger(transferToAnotherWard.getFromBedNo())){
			addFieldError("transferToAnotherWard.fromBedNo", "Incorrect bed number format");
		}
		if(transferToAnotherWard.getFromWardNo().length()==0){
			addFieldError("transferToAnotherWard.toWardNo", "This field is required");
		}
		if(transferToAnotherWard.getFromWardNo().length()!=0 && !this.isInteger(transferToAnotherWard.getFromWardNo())){
			addFieldError("transferToAnotherWard.toWardNo", "Incorrect ward number format");
		}
		if(transferToAnotherWard.getToBedNo().length()==0){
			addFieldError("transferToAnotherWard.toBedNo", "This field is required");
		}
		if(this.isInteger(transferToAnotherWard.getToBedNo())&& this.isInteger(transferToAnotherWard.getFromBedNo()) && this.isInteger(transferToAnotherWard.getToWardNo()) && this.isInteger(transferToAnotherWard.getFromWardNo())){
			if(transferToAnotherWard.getFromWardNo().equals(transferToAnotherWard.getToWardNo())){
				if(transferToAnotherWard.getToBedNo().equals(transferToAnotherWard.getFromBedNo())){
					addFieldError("transferToAnotherWard.toBedNo", "Destination bed is equal to the current bed");
				}
			}
		}
		if(transferToAnotherWard.getToBedNo().length()!=0 && !this.isInteger(transferToAnotherWard.getToBedNo())){
			addFieldError("transferToAnotherWard.toBedNo", "Incorrect bed number format");
		}
		if(transferToAnotherWard.getToWardNo().length()==0){
			addFieldError("transferToAnotherWard.toWardNo", "This field is required");
		}
		if(transferToAnotherWard.getToWardNo().length()!=0 && !this.isInteger(transferToAnotherWard.getToWardNo())){
			addFieldError("transferToAnotherWard.toWardNo", "Incorrect ward number format");
		}
		if(transferToAnotherWard.getTransferringOfficerId().length()==0){
			addFieldError("transferToAnotherWard.transferringOfficerId", "This field is required");
		}
		if(transferToAnotherWard.getReason().length()==0){
			addFieldError("transferToAnotherWard.reason", "This field is required");
		}

	}
	
	/**
	 * Check if the given string is an integer
	 * @param value
	 * @return true if the given string is an integer
	 */
	private Boolean isInteger(String value){
		Boolean isInteger=false;
		try{
			int intValue=Integer.parseInt(value);
			isInteger=true;
		}catch(NumberFormatException e){
			isInteger=false;
		}
		return isInteger;
	}
}
